#include "user_key.h"
#include "user_memory.h"

#undef	LOG_LVL
#define LOG_LVL		LOG_LVL_DEBUG
#include "log.h"

extern USER_PARA sys_user_para;
extern void para_set_auto_exit_clear(void);
extern void user_rgb_ctrl_set(uint16_t rst);
void user_para_adjust(PA_ID para_id, uint16_t step, uint8_t dir);

/* 参数调节步进值,第0行是短按按键时的步进值，第1行是长按按键时的步进值 */
uint16_t para_step_val[2][PARA_ID_MAX] = {
    {0, 1, 100, 100, 1,  1},
    {0, 5, 5*1000, 5*1000, 10, 1},
};

//功能按键句柄
struct Button btn_add;
struct Button btn_cut;
struct Button btn_set;
struct Button btn_enter;
struct Button btn_sen_a;
struct Button btn_sen_b;

/*
**功能按键键值获取回调函数
**1.所有功能按键共享该函数，其中5位按键位独立IO获取
**2.注意组合键应单独创建按键句柄，单独创建键值获取函数
**3.注意组合按键键值获取应当与该组合按键下的所有单独按键的键值获取相互斥，也就是组合按键出发时，其内的单独按键必然不应触发，反之也正确
**4.注意返回有效值与注册按键有效值一致的问题
*/
uint8_t read_btn_function(struct Button *btn)
{
    if(btn == &btn_add)
	{
		if(BTN_ADD_PRESS_VAL)				return KEY_DOWN_VAL;
	}
    else if(btn == &btn_cut)
	{
		if(BTN_CUT_PRESS_VAL)				return KEY_DOWN_VAL;
	}
    else if(btn == &btn_set)
	{
		if(BTN_SET_PRESS_VAL)				return KEY_DOWN_VAL;
	}
    else if(btn == &btn_enter)
	{
		if(BTN_ENTER_PRESS_VAL)				return KEY_DOWN_VAL;
	}
    else if(btn == &btn_sen_a)
	{
		if(BTN_SEN_A_PRESS_VAL)				return KEY_DOWN_VAL;
	}
    else if(btn == &btn_sen_b)
	{
		if(BTN_SEN_B_PRESS_VAL)				return KEY_DOWN_VAL;
	}
	
    return KEY_UP_VAL;
}

/*
**F1-F4功能按键事件触发回调函数
**1.所有功能按键共享该函数
**2.注意按键事件的唯一性，某一时刻只能触发一种事件
*/
static void btn_function_event_callback(struct Button *btn, PressEvent event)
{
    /* 有任一按键按下时清除自动退出计数 */
//    if(event == PRESS_DOWN)
//        para_set_auto_exit_clear();

	if(btn == &btn_add)
	{
		if(event == SINGLE_CLICK)
        {
            user_para_adjust(sys_user_para.para_id, para_step_val[0][sys_user_para.para_id % PARA_ID_MAX], 1);
            user_para_set_display(sys_user_para.para_id, 0);
        }
        else if(event == LONG_PRESS_HOLD)
        {
            user_para_adjust(sys_user_para.para_id, para_step_val[1][sys_user_para.para_id % PARA_ID_MAX], 1);
            user_para_set_display(sys_user_para.para_id, 0);
        }
	}
	else if(btn == &btn_cut)
	{
		if(event == SINGLE_CLICK)
        {
            user_para_adjust(sys_user_para.para_id, para_step_val[0][sys_user_para.para_id % PARA_ID_MAX], 0);
            user_para_set_display(sys_user_para.para_id, 0);
        }
        else if(event == LONG_PRESS_HOLD)
        {
            user_para_adjust(sys_user_para.para_id, para_step_val[1][sys_user_para.para_id % PARA_ID_MAX], 0);
            user_para_set_display(sys_user_para.para_id, 0);
        }
	}
	else if(btn == &btn_set)
	{
		if(event == SINGLE_CLICK)
        {
            if(sys_user_para.para_id  < (PARA_ID_MAX - 1))
                sys_user_para.para_id++;
            else
                sys_user_para.para_id = PARA_ID_RGB_LENGTH;
            user_para_set_display(sys_user_para.para_id, 0);

            user_rgb_ctrl_set(0);
        }
	}
	else if(btn == &btn_enter)
	{
		if(event == SINGLE_CLICK)
        {
            if(sys_user_para.para_id != PARA_ID_NULL)
            {
                sys_user_para.para_id = PARA_ID_NULL;
                user_para_set_display(sys_user_para.para_id, 0);
                set_all_led_same_color(0, RGB_LENGTH_MAX);
                /* 写入用户配置参数 */
                memcpy(&sys_user_memory.cfg_para, &sys_user_para.cfg_para, sizeof(SYS_CFG));
                write_user_config_para(&sys_user_memory);
                delay_ms(50);
                user_rgb_ctrl_set(1);
            }
        }
	}
    else if(btn == &btn_sen_a)
	{
		if(event == PRESS_UP)
        {
            if(sys_user_para.para_id == PARA_ID_NULL)
            {
                sys_user_para.sensor_a_sta = TRUE;
                sys_user_para.sensor_a_trig_en = TRUE;
                user_seneor_sta_display_update(sys_user_para.sensor_a_sta, sys_user_para.sensor_b_sta);
            }
        }
        else if(event == PRESS_DOWN)
        {
            if(sys_user_para.para_id == PARA_ID_NULL)
            {
                sys_user_para.sensor_a_sta = FALSE;
                user_seneor_sta_display_update(sys_user_para.sensor_a_sta, sys_user_para.sensor_b_sta);
            }
        }
	}
    else if(btn == &btn_sen_b)
	{
		if(event == PRESS_UP)
        {
            if(sys_user_para.para_id == PARA_ID_NULL)
            {
                sys_user_para.sensor_b_trig_en = TRUE;
                sys_user_para.sensor_b_sta = TRUE;
                user_seneor_sta_display_update(sys_user_para.sensor_a_sta, sys_user_para.sensor_b_sta);
            }
        }
        else if(event == PRESS_DOWN)
        {
            if(sys_user_para.para_id == PARA_ID_NULL)
            {
                sys_user_para.sensor_b_sta = FALSE;
                user_seneor_sta_display_update(sys_user_para.sensor_a_sta, sys_user_para.sensor_b_sta);
            }
        }
	}
}

/**
 * @description: 用户参数调节
 * @param {PA_ID} para_id  参数ID
 * @param {uint16_t} step  步进值
 * @param {uint8_t} dir    调节方向 0：减 1：加
 * @return {*}
 */
void user_para_adjust(PA_ID para_id, uint16_t step, uint8_t dir)
{
    uint32_t rgb_color = 0;
    if(para_id == PARA_ID_NULL)
        return;

    if(para_id == PARA_ID_RGB_LENGTH)
    {
        if(dir)
            sys_user_para.cfg_para.rgb_length += step;
        else
        {
            if(sys_user_para.cfg_para.rgb_length > step)
                sys_user_para.cfg_para.rgb_length -= step;
            else
                sys_user_para.cfg_para.rgb_length = 0;
        }
        if(sys_user_para.cfg_para.rgb_length > RGB_LENGTH_MAX)
            sys_user_para.cfg_para.rgb_length = RGB_LENGTH_MAX;
        if(sys_user_para.cfg_para.rgb_length < RGB_LENGTH_MIN)
            sys_user_para.cfg_para.rgb_length = RGB_LENGTH_MIN;
        
        sys_user_para.rgb_flow_time_min = sys_user_para.cfg_para.rgb_length * 3 * 125 / 10;
        sys_user_para.rgb_flow_time_min = GET_UP_TO_N(sys_user_para.rgb_flow_time_min, 1000);
//        if(sys_user_para.cfg_para.flow_time < sys_user_para.rgb_flow_time_min)
//            sys_user_para.cfg_para.flow_time = sys_user_para.rgb_flow_time_min;

        sys_user_para.rgb_flow_step_time = sys_user_para.cfg_para.flow_time * 10 / sys_user_para.cfg_para.rgb_length / 3;

        log_d("rgb_length = %d", sys_user_para.cfg_para.rgb_length);
        log_d("rgb_flow_time_min = %dms", sys_user_para.rgb_flow_time_min);
        log_d("rgb_flow_step_time = %d", sys_user_para.rgb_flow_step_time);

        rgb_color = ((uint32_t)sys_user_para.rgb_light_color << 16) + ((uint32_t)sys_user_para.rgb_light_color << 8) + sys_user_para.rgb_light_color;
        set_rgb_led_same_color(rgb_color, sys_user_para.cfg_para.rgb_length);
    }
    else if(para_id == PARA_ID_FLOW_TIME)
    {
        if(dir)
            sys_user_para.cfg_para.flow_time += step;
        else
        {
            if(sys_user_para.cfg_para.flow_time > step)
                sys_user_para.cfg_para.flow_time -= step;
            else
                sys_user_para.cfg_para.flow_time = 0;
        }
        if(sys_user_para.cfg_para.flow_time > RGB_FLOW_TIME_MAX)
            sys_user_para.cfg_para.flow_time = RGB_FLOW_TIME_MAX;
//        if(sys_user_para.cfg_para.flow_time < sys_user_para.rgb_flow_time_min)
//            sys_user_para.cfg_para.flow_time = sys_user_para.rgb_flow_time_min;
        if(sys_user_para.cfg_para.flow_time < RGB_FLOW_TIME_MIN)
            sys_user_para.cfg_para.flow_time = RGB_FLOW_TIME_MIN;

        sys_user_para.rgb_flow_step_time = sys_user_para.cfg_para.flow_time * 10 / sys_user_para.cfg_para.rgb_length / 3;
        log_d("flow_time = %d", sys_user_para.cfg_para.flow_time);
        log_d("rgb_flow_step_time = %d", sys_user_para.rgb_flow_step_time);
    }
    else if(para_id == PARA_ID_ALL_ON_TIME)
    {
        if(dir)
            sys_user_para.cfg_para.all_on_time += step;
        else
        {
            if(sys_user_para.cfg_para.all_on_time > step)
                sys_user_para.cfg_para.all_on_time -= step;
            else
                sys_user_para.cfg_para.all_on_time = 0;
        }
        if(sys_user_para.cfg_para.all_on_time > RGB_ALL_ON_TIME_MAX)
            sys_user_para.cfg_para.all_on_time = RGB_ALL_ON_TIME_MAX;
        if(sys_user_para.cfg_para.all_on_time < RGB_ALL_ON_TIME_MIN)
            sys_user_para.cfg_para.all_on_time = RGB_ALL_ON_TIME_MIN;
        
        log_d("all_on_time = %d", sys_user_para.cfg_para.all_on_time);
    }
    else if(para_id == PARA_ID_RGB_BRIGHT)
    {
        if(dir)
            sys_user_para.cfg_para.rgb_bright += step;
        else
        {
            if(sys_user_para.cfg_para.rgb_bright > step)
                sys_user_para.cfg_para.rgb_bright -= step;
            else
                sys_user_para.cfg_para.rgb_bright = 0;
        }
        if(sys_user_para.cfg_para.rgb_bright > RGB_BRIGHT_MAX)
            sys_user_para.cfg_para.rgb_bright = RGB_BRIGHT_MAX;
        if(sys_user_para.cfg_para.rgb_bright < RGB_BRIGHT_MIN)
            sys_user_para.cfg_para.rgb_bright = RGB_BRIGHT_MIN;
        
        sys_user_para.rgb_light_color = sys_user_para.cfg_para.rgb_bright * 255 / 100;
        
        log_d("rgb_bright = %d", sys_user_para.cfg_para.rgb_bright);
        rgb_color = ((uint32_t)sys_user_para.rgb_light_color << 16) + ((uint32_t)sys_user_para.rgb_light_color << 8) + sys_user_para.rgb_light_color;
        set_rgb_led_same_color(rgb_color, sys_user_para.cfg_para.rgb_length);
    }
    else if(para_id == PARA_ID_RGB_FORMAT)
    {
        if(dir)
            sys_user_para.cfg_para.rgb_format += (RGB_FORMAT)step;
        else
        {
            if(sys_user_para.cfg_para.rgb_format > step)
                sys_user_para.cfg_para.rgb_format -= (RGB_FORMAT)step;
            else
                sys_user_para.cfg_para.rgb_format = (RGB_FORMAT)(RGB_FORMAT_MAX - 1);
        }
        if(sys_user_para.cfg_para.rgb_format >= RGB_FORMAT_MAX)
            sys_user_para.cfg_para.rgb_format = (RGB_FORMAT)(0);
        
        log_d("rgb_format = %d", sys_user_para.cfg_para.rgb_format);
    }
}


static void user_button_task(void)
{
	button_ticks();
}

//系统任务句柄(对象)
static struct Timer user_button_timer; 
void creat_user_button_task(void)
{
	button_init(&btn_add,    read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);
    button_init(&btn_cut,    read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);
    button_init(&btn_set,    read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);
    button_init(&btn_enter,  read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);
    button_init(&btn_sen_a,  read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);
    button_init(&btn_sen_b,  read_btn_function, KEY_DOWN_VAL, btn_function_event_callback);

	timer_init(&user_button_timer, user_button_task, 5, LOOP_FOREVER);
	timer_start(&user_button_timer);
}







